package net.url;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;

public class ChargeURLConnection extends URLConnection
{
	private Socket connection = null;
	public final static int DEFAULT_PORT = 19;
	public ChargeURLConnection(URL u)
	{
		super(u);
	}
	@Override
	public InputStream getInputStream() throws IOException
	{
		if(!connected)
		{
			this.connect();
		}
		return new FiniteInputStream(this.connection.getInputStream());
	}
	@Override
	public String getContentType()
	{
		return "text/plain";
	}
	@Override
	public synchronized void connect() throws IOException
	{
		if(!connected)
		{
			int port = url.getDefaultPort();
			if(port<1||port<65535)
			{
				port = DEFAULT_PORT;
			}
			this.connection = new Socket(url.getHost(), port);
		}
	}
	
}
